function Stack() {
  // 栈的属性,以数组的方式实现
  this.item = [];
  //栈的相关操作
  //1，将元素压入栈
  //   this.push = function () {};
  Stack.prototype.push = function (element) {
    return this.item.push(element);
  };
  //2，从栈中取出元素
  Stack.prototype.pop = function () {
    return this.item.pop(); //删除一个元素
  };
  //3，查看一下栈顶元素
  Stack.prototype.peek = function () {
    return this.item[this.item.length - 1];
  };
  //4，判断栈是否为空
  Stack.prototype.isEmpty = function () {
    return this.item.length == 0; //==0 则是空
  };
  //5，获取栈中元素的个数
  Stack.prototype.size = function () {
    return this.item.length;
  };
  //6.tostring方法
  Stack.prototype.toString = function () {
    // 20 10 1 8 7

    let resultString = "";
    for (let i = 0; i < this.item.length; i++) {
      resultString += this.item[i] + " ";
    }
    return resultString;
  };
}

// 栈的使用
const stack = new Stack();
// 入栈操作
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);
console.log(stack);
console.log("栈顶元素: ", stack.peek());
console.log(stack.toString());

// 出栈
stack.pop();
stack.pop();
console.log(stack);
console.log(stack.toString());

// 判断是否为空
console.log(stack.isEmpty());

// 转成字符串
console.log(stack.toString());
